package com.hystrix.controller;

import com.hystrix.command.UserCommand;
import com.hystrix.entity.User;
import com.hystrix.service.UserService;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.hystrix.HystrixRequestCache;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Random;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

@RestController
public class HystrixController {

    private static final Logger logger = LoggerFactory.getLogger(HystrixController.class);

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/base",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    public String base(){
        return userService.getBaseInfo();
    }

    /**
     * 调用自定义命令
     * */
    @RequestMapping(value = "/getUser",method = RequestMethod.GET)
    public User getUserInfo() throws ExecutionException, InterruptedException {
        HystrixRequestContext.initializeContext();

        UserCommand userCommand = new UserCommand(HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CommandKey"))
                                                    ,restTemplate,1);
        //同步调用
        User user = userCommand.execute();
        logger.info("同步调用："+user.getName());

        //异步调用
//        Thread.sleep(new Random().nextInt(50000));
//        Future<User> future = userCommand.queue();
//        future.get();
//        logger.info("异步调用："+future.get().getName());
        return user;
    }

    /**
     * Hystrix请求缓存
     * */
    @RequestMapping(value = "/getUserCache",method = RequestMethod.GET)
    public User getUserInfoCache() throws ExecutionException, InterruptedException {

        HystrixRequestContext.initializeContext();
        UserCommand userCommand1 = new UserCommand(HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CommandKey")),restTemplate,1);
        User user1 = userCommand1.execute();
        UserCommand userCommand2 = new UserCommand(HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CommandKey")),restTemplate,1);
        User user2 = userCommand2.execute();
        UserCommand userCommand3 = new UserCommand(HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CommandKey")),restTemplate,1);
        User user3 = userCommand3.execute();
        UserCommand userCommand4 = new UserCommand(HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CommandKey")),restTemplate,1);
        User user4 = userCommand4.execute();
        logger.info("User1："+user1);
        logger.info("User2："+user2);
        logger.info("User2："+user3);
        logger.info("User3："+user4);
        return user1;
    }
}
